package com.elepro.shoppingmall.mapper;

import com.elepro.shoppingmall.entity.Message;
import com.elepro.shoppingmall.entity.UserMessage;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MessageMapper {
    //增加消息
    @Insert("insert message(content, time) values(#{content}, #{time})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insertMessage(Message message);

    //向每个普通用户增加消息
    @Insert("insert user_message(user_id, message_id, status) values(#{userId}, #{messageId}, #{status})")
    void insertUserMessage(UserMessage userMessage);

    //删除消息
    @Delete("delete from message where id = #{id}")
    void deleteMessageById(long id);

    //向每个普通用户删除消息
    @Delete("delete from user_message where message_id = #{id}")
    int deleteUserMessageByMessageId(long id);

    //查询所有消息
    @Select("select * from message")
    List<Message> getAllMessage();

    //用户查询所有消息
    @Select("select message.* from message, user_message where user_id = #{userId}")
    List<Message> getUserAllMessage(long userId);
}
